<?php

use databaseClass\database;

//服务器和数据库信息
$servername = "localhost";
$username = "root";
$password = "";
$dbName = "myDB";
 
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbName);
 
// 检测连接
if ($conn->connect_error) {
  die("连接失败: " . $conn->connect_error);
}

/**********************************************************表格创建******************************************* */
// class Table{
//   const CREATETABLE = 'CREATE TABLE ';
//   var $sql = '';
//   // protected $tablename;

//   function sethead($tablename ,array $tableHead)
//   {
//     // $this->tablename = $tablename;
//     $tableHead = implode(",", $tableHead);
//     $this->sql = static::CREATETABLE . $tablename . "($tableHead)";
//   }

// }

// // 表头数据
// // $studengId = 'studengId VARCHAR(10)  NOT NULL PRIMARY KEY';
// $id = 'id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY';
// $firstName = 'firstname VARCHAR(30) NOT NULL';
// $grade = 'grade TINYINT UNSIGNED NOT NULL';
// // $age = 'age TINYINT UNSIGNED NOT NULL';
// $tableHead = array();
// array_push($tableHead,$id, $firstName,$grade);

// //表名
// $tableName = 'STUDENTGRADE';
// $student = new Table();
// $student->sethead($tableName,$tableHead);
// // echo($student->sql);

// //创建表

// if ($conn->query($student->sql) === true) {

//   echo ("Table $tableName created successfully");
// } else {
//   echo ("fail to creat Table $tableName" . PHP_EOL . $conn->error);
// }


/***************************************************插入数据************************************************ */
// class insert {
//   protected $head = [];
//   protected $data = [];
//   const insert = 'INSERT INTO ';
//   var $sql = '';
//   protected $tablename = '';

//   function __construct($tableName,array $head)
//   {
//     $this->tablename = $tableName;
//     $this->head = $head;
//   }
  
//   function set(array $data)
//   {
//     $this->data = array_values($data);
//   }
//   public function sethead(array $head)
//   {
//     $this->head = array_keys($headdata);
//   }

//    public function changeTable($tableName)
//   {
//     $this->tableName = $tableName;
//   }

//   public final function quertSql()
//   {
//     $this->sql .= static::insert . $this->tablename . "(" . implode(",",$this->head) . ") VALUES ('" . implode("','",$this->data) ."');";
//   }

// }
// $tablename = 'studentinfo';
// $head = ['studengId','firstname','age'];
// $student = new insert($tablename,$head);
// $data = ['2014110210','aasdd',103];
// $student->set($data);
// $student->quertSql();
// if ($conn->multi_query($student->sql) === true) {
//   echo ("insert data success");
// } else {
//   echo ("fail to insert data" . $conn->error);
// }
/***********************************************************多表查询****************************************************** */


$sql = "SELECT a.studengid ,a.firstname ,b.grade From studentinfo a INNER JOIN studentgrade b ON a.firstname = b.firstname";
$result = $conn->query($sql);
var_dump($result);
echo '<table border="1"><tr><td>学号</td><td>姓名</td><td>成绩</td></tr>';
while($row = $result->fetch_assoc()){
  echo "<tr><td> {$row['studengid']}</td> ".
         "<td>{$row['firstname']} </td> ".
         "<td>{$row['grade']} </td> ".
         "</tr>";
}
echo '</table>';
$conn->close();
?>